using System.Collections.Generic;
using UnityEngine;

public class AudioController : MonoBehaviour
{
	private int maxClipsAtOnce = 15;

	private GameObject[] soundObjects;

	private GameObject background;

	private Dictionary<string, AudioClip> clips = new Dictionary<string, AudioClip>();

	private UserPreferences userPreferences;

	private static AudioController _instance;

	public static AudioController instance
	{
		get
		{
			return GetInstance();
		}
		private set
		{
			_instance = value;
		}
	}

	private static AudioController GetInstance()
	{
		if (_instance == null)
		{
			FindOrCreateInstance();
		}
		return _instance;
	}

	private static void FindOrCreateInstance()
	{
		AudioController audioController = UnityEngine.Object.FindObjectOfType<AudioController>();
		if (audioController != null)
		{
			_instance = audioController;
		}
		else
		{
			CreateNewAudioController();
		}
	}

	private static void CreateNewAudioController()
	{
		GameObject utilitiesGameObject = GetUtilitiesGameObject();
		AudioController audioController = _instance = utilitiesGameObject.AddComponent<AudioController>();
	}

	private static GameObject GetUtilitiesGameObject()
	{
		GameObject gameObject = GameObject.Find("Utilities");
		if (gameObject == null)
		{
			gameObject = new GameObject("Utilities");
			Object.DontDestroyOnLoad(gameObject);
		}
		return gameObject;
	}

	public GameObject GetBackground()
	{
		return background;
	}

	public GameObject[] GetSounds()
	{
		return soundObjects;
	}

	private void Awake()
	{
		soundObjects = new GameObject[maxClipsAtOnce];
		AudioClip[] array = Resources.LoadAll<AudioClip>("Sounds/");
		foreach (AudioClip audioClip in array)
		{
			clips.Add(audioClip.name, audioClip);
		}
		for (int j = 0; j < soundObjects.Length; j++)
		{
			soundObjects[j] = new GameObject();
			soundObjects[j].name = "EmptyAudioClip";
			soundObjects[j].AddComponent<AudioSource>();
			soundObjects[j].AddComponent<SoundController>();
			soundObjects[j].transform.SetParent(base.transform);
		}
		background = new GameObject();
		background.AddComponent<AudioSource>();
		background.transform.SetParent(base.transform);
		background.name = "background";
		userPreferences = UserPreferences.instance;
	}

	public void SetMaxClipc(int count)
	{
		maxClipsAtOnce = count;
	}

	public void PlayBackground(string name)
	{
		if (clips.ContainsKey(name))
		{
			if (!background.GetComponent<AudioSource>().isPlaying || !(background.GetComponent<AudioSource>().clip == clips[name]))
			{
				background.GetComponent<AudioSource>().clip = clips[name];
				background.GetComponent<AudioSource>().loop = true;
				background.GetComponent<AudioSource>().Play();
			}
			if (!(userPreferences == null))
			{
				background.GetComponent<AudioSource>().mute = !userPreferences.playerData.music;
			}
		}
	}

	public void MuteBackgroundMusic()
	{
		background.GetComponent<AudioSource>().mute = true;
	}

	public void PauseBackground()
	{
		background.GetComponent<AudioSource>().Pause();
	}

	public void UnPauseBackground()
	{
		if (UserPreferences.instance.playerData.music)
		{
			background.GetComponent<AudioSource>().Play();
		}
	}

	public void EnableDisableMusic()
	{
		if (!(userPreferences == null))
		{
			background.GetComponent<AudioSource>().mute = !userPreferences.playerData.music;
		}
	}

	public void EnableDisableAllSounds()
	{
		if (!(userPreferences == null))
		{
			for (int i = 0; i < soundObjects.Length; i++)
			{
				soundObjects[i].GetComponent<AudioSource>().mute = !userPreferences.playerData.sound;
			}
		}
	}

	public void DisableAllSounds()
	{
		for (int i = 0; i < soundObjects.Length; i++)
		{
			soundObjects[i].GetComponent<AudioSource>().mute = true;
		}
	}

	public void StopAllSounds()
	{
		for (int i = 0; i < soundObjects.Length; i++)
		{
			soundObjects[i].GetComponent<AudioSource>().Stop();
		}
	}

	public void StopSounds(string clipName)
	{
		for (int i = 0; i < soundObjects.Length; i++)
		{
			GameObject gameObject = soundObjects[i];
			AudioSource component = gameObject.GetComponent<AudioSource>();
			if (component != null && component.clip != null && component.clip.name == clipName)
			{
				component.Stop();
			}
		}
	}

	public GameObject Play(string name, GameObject obj = null, bool multipleScenes = false, bool loop = false)
	{
		if (userPreferences == null)
		{
			return null;
		}
		if (userPreferences.playerData == null)
		{
			return null;
		}
		if (!userPreferences.playerData.sound)
		{
			return null;
		}
		if (!clips.ContainsKey(name))
		{
			return null;
		}
		GameObject gameObject = Exist(name);
		AudioClip clip = clips[name];
		if (gameObject == null || gameObject.GetComponent<AudioSource>().isPlaying)
		{
			for (int i = 0; i < soundObjects.Length; i++)
			{
				if (!soundObjects[i].GetComponent<AudioSource>().isPlaying)
				{
					gameObject = soundObjects[i];
					SoundController component = gameObject.GetComponent<SoundController>();
					AudioSource component2 = gameObject.GetComponent<AudioSource>();
					gameObject.name = name;
					if (obj != null)
					{
						gameObject.transform.SetParent(obj.transform);
					}
					component.SetMultipleScenes(multipleScenes);
					component2.loop = loop;
					component2.clip = clip;
					break;
				}
			}
		}
		if (gameObject != null)
		{
			SoundController component3 = gameObject.GetComponent<SoundController>();
			AudioSource component4 = gameObject.GetComponent<AudioSource>();
			gameObject.name = name;
			if (obj != null)
			{
				gameObject.transform.SetParent(obj.transform);
			}
			component3.SetMultipleScenes(multipleScenes);
			component4.loop = loop;
			component4.clip = clip;
			gameObject.GetComponent<AudioSource>().Play();
		}
		return gameObject;
	}

	public GameObject Exist(string name)
	{
		GameObject[] array = soundObjects;
		foreach (GameObject gameObject in array)
		{
			if (!(gameObject.GetComponent<AudioSource>().clip == null) && gameObject.GetComponent<AudioSource>().clip.name == name)
			{
				return gameObject;
			}
		}
		return null;
	}

	public void SetVolume(float level, GameObject obj)
	{
	}

	public void SetPitch(float level, GameObject obj)
	{
		obj.GetComponent<AudioSource>().pitch = level;
	}

	public void SetPitch(float goalLevel, float time, AudioSource obj)
	{
		if (obj.pitch > goalLevel)
		{
			obj.pitch -= Time.deltaTime * Mathf.Abs(goalLevel) / time;
		}
		else if (obj.pitch < goalLevel)
		{
			obj.pitch += Time.deltaTime * Mathf.Abs(goalLevel) / time;
		}
	}

	public AudioClip GetClip(string name)
	{
		return clips[name];
	}

	public bool isBackgroundPlaying(string name)
	{
		AudioSource component = background.GetComponent<AudioSource>();
		if (component == null)
		{
			return false;
		}
		if (component.clip == null)
		{
			return false;
		}
		if (component.clip.name == name && component.isPlaying && !component.mute)
		{
			return true;
		}
		return false;
	}

	public bool BackgroundExists(string name)
	{
		AudioSource component = background.GetComponent<AudioSource>();
		if (component == null)
		{
			return false;
		}
		if (component.clip == null)
		{
			return false;
		}
		return component.clip.name == name;
	}
}
